# -*- coding = utf-8 -*-
# @Time : 2022/11/17 11:21
# @Author : GHHHHHHHHH
# @File : ViewDao.py
# @Software : PyCharm
import datetime

from dao import db


def save_layer_log(uuid, s):
    sql = f'insert into layerLog (uuid, file_path, create_time) values ("%s", "%s", "%s")' % (
        uuid, s.replace('\\', '\\\\'), datetime.datetime.now())
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
        return False
    else:
        return True


def save_train_log(uuid, s):
    sql = f'insert into trainLog (uuid, file_path, create_time) values ("%s", "%s", "%s")' % (
        uuid, s.replace('\\', '\\\\'), datetime.datetime.now())
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
        return False
    else:
        return True


def save_pre_log(uuid, s):
    sql = f'insert into preLog (uuid, file_path, create_time) values ("%s", "%s", "%s")' % (
        uuid, s.replace('\\', '\\\\'), datetime.datetime.now())
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
        return False
    else:
        return True


def get_all_layer_logs(uuid):
    sql = 'select id, uuid, file_path, create_time from layerLog where uuid = "%s"' % uuid
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path', "create_time"], data1)) for data1 in data]


def get_all_train_logs(uuid):
    sql = 'select id, uuid, file_path, create_time from trainLog where uuid = "%s"' % uuid
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path', "create_time"], data1)) for data1 in data]


def get_all_pre_logs(uuid):
    sql = 'select id, uuid, file_path, create_time from preLog where uuid = "%s"' % uuid
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path', "create_time"], data1)) for data1 in data]


def get_train_log_by_id(_id):
    sql = 'select id, uuid, file_path from trainLog where id = "%s"' % _id
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path'], data1)) for data1 in data]


def get_layer_log_by_id(_id):
    sql = 'select id, uuid, file_path from layerLog where id = "%s"' % _id
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path'], data1)) for data1 in data]


def get_pre_log_by_id(_id):
    sql = 'select id, uuid, file_path from preLog where id = "%s"' % _id
    data = db.session.execute(sql).fetchall()
    return [dict(zip(['id', 'uuid', 'file_path'], data1)) for data1 in data]


def delete_train_log_by_id(_id):
    sql = 'delete from trainLog where id = "%s"' % _id
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        return False
    else:
        return True


def delete_layer_log_by_id(_id):
    sql = 'delete from layerLog where id = "%s"' % _id
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        return False
    else:
        return True


def delete_pre_log_by_id(_id):
    sql = 'delete from preLog where id = "%s"' % _id
    try:
        db.session.execute(sql)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        return False
    else:
        return True
